﻿using System;
using System.Collections.Generic;
using System.Text;
using WordLib.Model;
using WordService.Model;
using System.Text.RegularExpressions;

namespace WordService.Utility
{
    public class ViewUtil
    {
        private const String regStr = @"\d{4,4}";
        private const String insRegStr = @"\d{1}";
        // 根据视图模型解析成为数据模型
        public static IList<Article> GetCreateModelList(IList<InputDataModel> dataList)
        {
            IList<Article> articleList = new List<Article>();
            foreach (InputDataModel data in dataList)
            {
                Article article = new Article();
                // 全名的解析
                Int32 lastIndex = data.FullName.IndexOf('.');
                article.FullName = data.FullName.Substring(0,lastIndex);
                // 时间的解析
                Regex regex = new Regex(regStr);
                if (regex.IsMatch(data.FullName))
                {
                    Match match = regex.Match(data.FullName);
                    article.PublishDate = new DateTime(Int32.Parse(match.Value), 1, 1);
                }
                // 普通不需要解析的属性
                article.Title = data.Title;
                article.ISSNNumber = data.ISSN;
                article.CNNumber = data.CN;
                article.CollectionNumber = data.Collect;
                article.Summary = data.Summary;
                article.Category = data.Category;
                // 关键字属性解析
                String[] collections = data.Keyword.Split(' ');
                article.KeyWord = new List<Keyword>();
                foreach (String item in collections)
                {
                    Keyword keyword = new Keyword();
                    keyword.Name = item;
                    keyword.RightRate = 0.0;
                    keyword.TotalCount = 0;
                    keyword.Count = 0;
                    article.KeyWord.Add(keyword);
                }
                // 机构分析
                regex = new Regex(insRegStr);
                article.Institution = new List<Institution>();
                String[] institutions = data.Institution.Split(' ');
                // 如果只有一个机构的两种情况
                if (institutions.Length == 1)
                {
                    Institution institution = new Institution();
                    if (regex.IsMatch(institutions[0]))
                    {
                        Match match = regex.Match(institutions[0]);
                        institution.Sequence = Convert.ToInt32(match.Value);
                        if (institutions[0].Contains("[") || institutions[0].Contains("]"))
                        {
                            institution.Name = institutions[0].Split(']')[1];
                        }
                        else
                        {
                            institution.Name = institutions[0];
                        }
                    }
                    else
                    {
                        institution.Sequence = 1;
                        institution.Name = institutions[0];
                    }
                    // >> 建立文章到机构的关联
                    article.Institution.Add(institution);
                }
                // 拥有了两个机构以上的情况
                else
                {
                    foreach (String item in institutions)
                    {
                        Institution institution = new Institution();
                        if (regex.IsMatch(item))
                        {
                            Match match = regex.Match(item);
                            institution.Sequence = Convert.ToInt32(match.Value);
                            if (item.Contains("[") || item.Contains("]"))
                            {
                                institution.Name = item.Split(']')[1];
                            }
                            else
                            {
                                institution.Name = item;
                            }
                            // >> 建立文章到机构的关联
                            article.Institution.Add(institution);
                        }
                    }
                }
                // 作者解析
                article.Author = new List<Author>();
                String[] authors = data.Author.Split(' ');
                if (authors.Length == 1)
                {
                    Author author = new Author();
                    author.Institutions = new List<Institution>();
                    author.TotalCount = 1L;

                    if (authors[0].Contains("[") || authors[0].Contains("]"))
                    {
                        author.Name = authors[0].Split('[')[0];
                        author.Sequence = 1;
                    }
                    else
                    {
                        author.Name = authors[0];
                        author.Sequence = 1;
                    }
                    if (article.Institution.Count == 1)
                    {
                        // 机构作者关联
                        ((List<Institution>)author.Institutions).AddRange(article.Institution);
                        article.Institution[0].Authors = new List<Author>();
                        article.Institution[0].Authors.Add(author);
                    }
                    else
                    {
                        // 一个作者两个机构
                        if (authors[0].Contains("[") || authors[0].Contains("]"))
                        {
                            foreach (Institution institution in article.Institution)
                            {
                                if (authors[0].Contains(institution.Sequence.ToString()))
                                {
                                    author.Institutions.Add(institution);
                                    institution.Authors = new List<Author>();
                                    institution.Authors.Add(author);
                                }
                            }
                        }
                    }
                    // >> 文章建立到作者的关联
                    article.Author.Add(author);
                }
                else
                {
                    Int32 index = 1;
                    foreach (String item in authors)
                    {
                        Author author = new Author();
                        author.Institutions = new List<Institution>();
                        author.TotalCount = 1L;

                        if (item.Contains("[") || item.Contains("]"))
                        {
                            author.Name = item.Split('[')[0];
                            author.Sequence = index;
                        }
                        else
                        {
                            author.Name = item;
                            author.Sequence = index;
                        }

                        foreach (Institution institution in article.Institution)
                        {
                            if (item.Contains(institution.Sequence.ToString()))
                            {
                                author.Institutions.Add(institution);
                                institution.Authors = new List<Author>();
                                institution.Authors.Add(author);
                            }
                        }
                        // >> 文章建立到作者的关联
                        article.Author.Add(author);
                        index++;
                    }
                }
                // 构建文章的List
                articleList.Add(article);
            }
            return articleList;
        }
        /// <summary>
        /// 是否存在包含关系的列表判断方法
        /// </summary>
        /// <param name="inputList"></param>
        /// <param name="inputValue"></param>
        /// <returns></returns>
        public static Boolean IsContain(IList<InputDataModel> inputList, InputDataModel inputValue)
        {
            Boolean result = false;
            foreach (InputDataModel data in inputList)
            {
                if (data.Title.Trim().Equals(inputValue.Title.Trim()))
                {
                    result = true;
                    break;
                }
            }
            return result;
        }
        /// <summary>
        /// 去除重复的判断方法
        /// </summary>
        /// <param name="dataModel"></param>
        /// <returns></returns>
        public static Boolean IsComplete(InputDataModel dataModel)
        {
            Boolean result = true;
            if (dataModel.Title == null || dataModel.Title.Trim().Equals(String.Empty))
            {
                result = false;
            }
            if (dataModel.Author == null || dataModel.Author.Trim().Equals(String.Empty))
            {
                result = false;
            }
            if (dataModel.Category == null || dataModel.Category.Trim().Equals(String.Empty))
            {
                result = false;
            }
            if (dataModel.CN == null || dataModel.CN.Trim().Equals(String.Empty))
            {
                result = false;
            }
            if (dataModel.Collect == null || dataModel.Collect.Trim().Equals(String.Empty))
            {
                result = false;
            }
            if (dataModel.FullName == null || dataModel.FullName.Trim().Equals(String.Empty))
            {
                result = false;
            }
            if (dataModel.Institution == null || dataModel.Institution.Trim().Equals(String.Empty))
            {
                result = false;
            }
            if (dataModel.ISSN == null || dataModel.ISSN.Trim().Equals(String.Empty))
            {
                result = false;
            }
            if (dataModel.Keyword == null || dataModel.Keyword.Trim().Equals(String.Empty))
            {
                result = false;
            }
            if (dataModel.Summary == null || dataModel.Summary.Trim().Equals(String.Empty))
            {
                result = false;
            }
            return result;
        }
    }
}
